/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package progdynamique;

/**
 * Solution for a knapsack problem.
 * 
 * @author Guillaume
 */
public class Solution {
    
    private double[] itemsChoosen; //list of all items choosen (itemsChoosen[i]=1 (=0) => item i is choosen (=or not))
                                   //it's an array of double because we want that a relaxation could be a solution, and
                                   //relaxation may choose a part of an item in a solution.
    
    /**
     * Construct a new empty solution for knapsack problem.
     * <p>
     * No items are choosen.
     * 
     * @param nbItems number of items in the problem.
     */
    public Solution(int nbItems){
        
        this.itemsChoosen = new double[nbItems];
        
        for (int i = 0; i < itemsChoosen.length; i++) {
            itemsChoosen[i]=0;
        }
    }
    
    /**
     * Construct a Solution for knapsack problem from a resolution.
     * 
     * @param itemsChoosen the list of items choosen. (itemsChoosen[i]=1 => item i is choosen)
     */
    public Solution(double[] itemsChoosen){

        this.itemsChoosen = itemsChoosen;
    }
    
    /**
     * Construct a new Solution from a Solution (deep copy).
     * @param sol the Solution to copy.
     */
    public Solution(Solution sol){
        
        this.itemsChoosen = new double[sol.itemsChoosen.length];
        System.arraycopy(sol.itemsChoosen, 0, this.itemsChoosen, 0, sol.itemsChoosen.length);
    }
    
    /**
     * Give the total weight of the solution (sum of all object weight choosen in the solution).
     * 
     * @param dat the data from which object are choosen.
     * @return total weight of the solution.
     */
    public double weight(DataProblem dat){
        double weight = 0.0;
        for (int i = 0; i < itemsChoosen.length; i++) {
            weight += itemsChoosen[i]*dat.getListItems().get(i).getWeight();
        }
        return weight;
    }
    
    /**
     * Give the total profit of the solution (sum of all object profit choosen in the solution).
     * 
     * @param dat the data from which object are choosen.
     * @return total profit of the solution.
     */
    public double profit(DataProblem dat){
        double profit = 0.0;
        for (int i = 0; i < itemsChoosen.length; i++) {
            profit += itemsChoosen[i]*dat.getListItems().get(i).getProfit();
        }
        return profit;
    }

    public double[] getItemsChoosen() {
        return itemsChoosen;
    }
    
    @Override
    public String toString(){
       String result = "x = [";
        for (int i = 0; i < itemsChoosen.length; i++) {
            if(itemsChoosen[i]==1 || itemsChoosen[i]==0){
                result += " " + (int)this.itemsChoosen[i];
            }else{
                result += " " + this.itemsChoosen[i];
            }
        }
        
        result += " ]";
        return result;
    }
    
}
